<?PHP

/*
 * 操作用户信息
 * 杨继康
 * 2012/07/22
 */

class Users_Model extends CI_Model {

    //重载CI_Model
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('Role_Model');
    }

    //用户登陆验证
    function login_users($username = FALSE, $userpassword = FALSE) {

        //拼接where语句
        $array = array('u_name' => $username, 'u_password' => $userpassword);

        $query = $this->db->get_where('db_user', $array);

        $rows = $query->num_rows();
        if ($rows > 0) {
            $userContent = $query->result_array();
            $role = $this->Role_Model->GetEntity($userContent[0]["u_id"]);
            //获取是用户ID
            $this->session->set_userdata('uid', $userContent[0]["u_id"]);
            //输入正确，登陆并保存用户名到session；
            $this->session->set_userdata('uName', $userContent[0]["u_userName"]);
            //获取是否为管理员权限
            $this->session->set_userdata('uadmin', $userContent[0]["u_admin"]);
            //获取权限具体内容
            $this->session->set_userdata('role', $role[0]["ro_content"]);
        }
        //返回结果
        return $rows;
    }

    //通过用户ID、密码获取数据信息
    function getUserForPassword($userid = FALSE, $userpassword = FALSE) {
        //拼接where语句
        $array = array('u_id' => $userid, 'u_password' => $userpassword);

        $query = $this->db->get_where('db_user', $array);

        $rows = $query->num_rows();
        //返回结果
        return $rows;
    }

    //获取用户列表
    function get_users($str = FALSE) {
        $this->db->order_by("u_id", "desc");
        $query = $this->db->get_where('db_user', array('u_state' => $str));
        //返回结果
        return $query->result_array();
    }

    /*
     * Insert_User
     * 写入数据库
     */

    function Insert_User($data = FALSE) {
        $result = $this->db->insert('db_user', $data);
        return $result;
    }

    /*
     * GetEntity
     * 获取单挑数据
     * 参数：u_id
     */

    function GetEntity($str = NULL) {
        if ($str != NULL) {
            $query = $this->db->get_where('db_user', array('u_id' => $str));
            //返回结果
            return $query->result_array();
        } else {
            $Array = array(array("u_id" => '', "u_name" => '', "u_password" => '', "u_admin" => 0));
            return $Array;
        }
    }

    /*
     * GetEntity
     * 获取单挑数据
     * 参数：u_name
     */

    function GetEntityForName($str = NULL) {
        $query = $this->db->get_where('db_user', array('u_name' => $str));
        //返回结果
        return $query->num_rows();
    }

    /*
     * UpdateEntity
     * 更新单挑数据
     */

    function UpdateEntity($id = FALSE, $data = FALSE) {

        $this->db->where('u_id', $id);
        $result = $this->db->update('db_user', $data);
        //返回结果
        return $result;
    }

}

?>